var express = require('express');
var router = express.Router();
var Activity = require('../models/activity.js').activity;
var Mongoose = require('../models/mongodb.js');
var Comment = require('../models/activity.js').comment;
// 查找活动的评论
router.get('/', function(req, res) {
    console.log('hehe');
    var count = req.query.count;
    var id = req.query.id;
    var start = req.query.start?mongoose.Types.ObjectId(req.query.start):null;
    if (count && id) {
        Activity.findCommentByIDWithLimit(id, count,start, function(err, doc) {
           if (err) {
               res.send('errror');
           } else {
               res.send(doc);
           }
        });
    } else {
        res.send('param error');
    }
})

// 发送评论请求
router.post('/', function(req, res) {
    var activity = req.body.activity;
    var comment = req.body.comment;
    var author = req.body.author;
    var content = req.body.content;
    console.log(activity + ' ' + comment + ' ' + author + ' ' + content + ' ' + req.body);
    if (activity) {   // 直接评论
        if (author && content) {
            Activity.insertCommentByID(Mongoose.Types.ObjectId(activity)
                ,null
                ,Mongoose.Types.ObjectId(author)
                ,content, function(error, doc) {
                    if (error) {
                        res.send({code:1, description:'发布错误'});
                    } else {
                        res.send({code:0, description:'发布成功'});
                    }
                })
        } else {
            res.send('activity, 参数错误');
        }
    } else if (comment) {    //评论别人的评论
        if (author && content) {
            Comment.insertCommentForID(null,
                content,
                Mongoose.Types.ObjectId(author),
                Mongoose.Types.ObjectId(comment),
                Mongoose.Types.ObjectId(activity),
                function(err,doc){
                    if (err) {
                        res.send({code:1, description:'发布错误'});
                    } else {
                        res.send({code:0, description:'发布成功'});
                    }
                })
        } else {
            res.send('comment, 参数错误');
        }
    } else {    // 错误
        res.send('参数错误');
    }

});
module.exports = router;
